﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ZG_member
{
    public partial class goodsList : Form
    {
        public goodsList(MemberSaleForm sale)
        {
            InitializeComponent();
            fm = sale;
            this.Opacity = 0;
        }
        MemberSaleForm fm;
        private void Loaddgv()
        {

            foreach (DataGridViewRow dgv in dgvgoods.Rows)
            {
                ZG_member.BLL.goodsType goodtype = new ZG_member.BLL.goodsType();
                Model.goodsType goodtypeModel = new Model.goodsType();
                goodtypeModel = goodtype.GetModelList("id='" + dgv.Cells["goodsTypeId"].Value + "'")[0];
                dgv.Cells["typeName1"].Value = goodtypeModel.typeName;
            }
        }
        public void cuxiaoxianshi()
        {
            for (int i = 0; i < dgvgoods.Rows.Count; i++)
            {
                if (bool.Parse(dgvgoods.Rows[i].Cells["是否促销"].Value.ToString()))
                {
                    dgvgoods.Rows[i].Cells["price"].Value = "-";
                }
                else
                {
                    dgvgoods.Rows[i].Cells["促销售价"].Value = "-";
                }
            }
        }
        TreeNode node = new TreeNode();
        private void goodsList_Load(object sender, EventArgs e)
        {
            this.timer1.Start();
            //TreeView加载数据
            node.Text = "所有产品类别";//可编辑状态。   ，这个结点是根节点。 
            treeView1.Nodes.Add(node);
            ZG_member.BLL.goodsType gdType = new ZG_member.BLL.goodsType();
            DataTable dt= gdType.GetList("").Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                treeView1.Nodes.Add(dt.Rows[i]["typeName"].ToString());
            }

            //DGV加载数据
            ZG_member.BLL.goods good = new ZG_member.BLL.goods();
            DataTable dtt = good.GetList("").Tables[0];
            dgvgoods.DataSource = dtt;
            Loaddgv();
            cuxiaoxianshi();                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode.Text != "所有产品类别")
            {
                ZG_member.BLL.goodsType goodtype = new ZG_member.BLL.goodsType();
                Model.goodsType goodtypeModel = new Model.goodsType();
                goodtypeModel = goodtype.GetModelList("typeName='" + treeView1.SelectedNode.Text + "'")[0];


                ZG_member.BLL.goods good = new ZG_member.BLL.goods();
                DataTable dt = good.GetList("goodsTypeId='" + goodtypeModel.id + "'").Tables[0];
                dgvgoods.DataSource = dt;

                dgvgoods.AllowUserToAddRows = false;
                Loaddgv();
            }
            if (treeView1.SelectedNode.Text == "所有产品类别")
            {
                ZG_member.BLL.goods good = new ZG_member.BLL.goods();
                DataTable dtt = good.GetList("").Tables[0];
                dgvgoods.DataSource = dtt;

                dgvgoods.AllowUserToAddRows = false;
                Loaddgv();
            }
        }

        private void dgvgoods_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.textBox1.Text = dgvgoods.CurrentRow.Cells["goodsId"].Value.ToString();
            this.textBox2.Text = dgvgoods.CurrentRow.Cells["goodsName"].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (txtCount.Text=="")
            {
                return;
            }

            ZG_member.BLL.goods good = new ZG_member.BLL.goods();
            ZG_member.Model.goods gdModel = new ZG_member.Model.goods();
            gdModel= good.getModelGoodsIdString(this.textBox1.Text);
            if (gdModel.amount < Convert.ToInt32(this.txtCount.Text))
            {
                MessageBox.Show("超出库存数量！");
                return;
            }
            else
            {
                
                DataGridViewRow row = new DataGridViewRow();
                foreach (DataGridViewColumn c in dataGridView1.Columns)
                {
                    row.Cells.Add(c.CellTemplate.Clone() as DataGridViewCell);//给行添加单元格
                }

                row.Cells[0].Value=gdModel.goodsId.ToString();
                row.Cells[1].Value=gdModel.id;
                row.Cells[2].Value=gdModel.goodsTypeId;
                row.Cells[4].Value=gdModel.goodsName;
                row.Cells[5].Value=gdModel.price;
                row.Cells[6].Value = gdModel.Promotionprice;
                row.Cells[7].Value = gdModel.Promotion;
                row.Cells[8].Value = gdModel.goodsTypeId;
                row.Cells[9].Value = amount;
                this.dataGridView1.Rows.Add(row);
                foreach (DataGridViewRow dgv in dataGridView1.Rows)
                {
                    ZG_member.BLL.goodsType goodtype = new ZG_member.BLL.goodsType();
                    Model.goodsType goodtypeModel = new Model.goodsType();
                    goodtypeModel = goodtype.GetModelList("id='" + dgv.Cells["dataGridViewTextBoxColumn3"].Value + "'")[0];
                    dgv.Cells["dataGridViewTextBoxColumn4"].Value = goodtypeModel.typeName;
                }

                for (int i = 0; i < dataGridView1.Rows.Count; i++)
                {
                    if (bool.Parse(dataGridView1.Rows[i].Cells["dataGridViewCheckBoxColumn1"].Value.ToString()))
                    {
                        dataGridView1.Rows[i].Cells["dataGridViewTextBoxColumn6"].Value = "-";
                    }
                    else
                    {
                        dataGridView1.Rows[i].Cells["dataGridViewTextBoxColumn7"].Value = "-";
                    }
                }

            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        //private void button2_Click(object sender, EventArgs e)
        //{

        //    for (int i = 0; i < dataGridView1.Rows.Count; i++)
        //    {
        //        fm.cboGoods.Text = this.dataGridView1.Rows[i].Cells["dataGridViewTextBoxColumn5"].Value.ToString();
        //        fm.numericUpDown1.Value =Convert.ToInt32(this.dataGridView1.Rows[i].Cells["dataGridViewTextBoxColumn9"].Value.ToString());
        //        fm.button1_Click(sender, e);
        //    }
        //    this.Close();
        //}

        private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            this.dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.Left += 2;
            this.Opacity += 0.1;
            if (Opacity == 1)
            {
                this.timer1.Stop();

            }
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //timer关闭
            this.Top = this.Top + 10;
            this.Opacity = this.Opacity - 0.06;
            if (Opacity <= 0)
            {
                this.timer2.Stop();
                this.Close();

            }
        }
        int flag = 0;
        private void goodsList_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (flag == 0)
            {
                e.Cancel = true;
                flag = 1;
                this.timer2.Start();
            }
        }
    }
}
